﻿@model Matrix.MaKaYunShang.Model.CommodityLib

@{
    ViewBag.Title = "编辑库商品";
    ViewBag.PathToBtn = "/CommodityLib/List";
    ViewBag.TextBtn = "返回至列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<!-- /.row -->
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                库商品信息
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("Update", "CommodityLib", FormMethod.Post, new { id = "frmUpdate", enctype = "multipart/form-data" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-lg-12">
                            <fieldset>
                                @Html.HiddenFor(model => model.Id)
                                @Html.HiddenFor(model => model.HeadPic)
                                @Html.HiddenFor(model => model.AddTime)
                                @Html.HiddenFor(model => model.IsDelete)
                                <div class="form-group col-md-12">
                                    <input id="fileImage" class="form-control" type="file" name="fileImage" style="display: none;" />
                                    <input id="btnSelect" type=button class="btn btn-info" value="上传列表图" onclick="fileImage.click()" />
                                    <img id="imgThumb" alt="缩略图" style="max-height: 80px" src="/Content/admin/image/uploadimg.png" imgpath="/Content/admin/image/uploadimg.png" />
                                    <span>（建议尺寸：宽180*高180，200KB以内）</span>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>名称</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.CommodityName, new { @class = "form-control", placeholder = "名称" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>品牌</label>
                                    <div class="input-group">
                                        @Html.DropDownList("BrandId", (IEnumerable<SelectListItem>)ViewBag.Brands, new { @class = "form-control" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>建议售价</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.SuggestedPrice, new { @class = "form-control", placeholder = "售价" })
                                        <span class="input-group-addon">元</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>类别</label>
                                    <div class="input-group">
                                        @Html.DropDownList("CateId", (IEnumerable<SelectListItem>)ViewBag.Categories, new { @class = "form-control" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>单位</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Unit, new { @class = "form-control", placeholder = "单位" })
                                        <span class="input-group-addon">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>规格</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Spec, new { @class = "form-control", placeholder = "规格" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>下限价格</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.LowerPrice, new { @class = "form-control", placeholder = "下限价格" })
                                        <span class="input-group-addon">元</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>上限价格</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.CeilingPrice, new { @class = "form-control", placeholder = "上限价格" })
                                        <span class="input-group-addon">元</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>产地区域</label>
                                    <div class="input-group">
                                        <select id="ddlProvince" class="form-control" ></select>
                                        <select id="ddlCity" class="form-control"></select>
                                        @Html.DropDownList("ProducingAreaId", (IEnumerable<SelectListItem>)ViewBag.Areas, new { @class = "form-control" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>描述</label>
                                    <div class="input-group">
                                        @Html.TextAreaFor(model => model.Description, new { @class = "form-control", rows = "5", placeholder = "描述" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>销量</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.SalesVolume, new { @class = "form-control", placeholder = "销量" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-1">
                                    <label>是否为虚拟商品</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsVirtual)
                                    </div>
                                </div>
                                <div class="form-group col-md-1">
                                    <label>是否支持立减</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsSupportDiscount)
                                    </div>
                                </div>
                                <div class="form-group col-md-1">
                                    <label>是否为自营商品</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsLanJianCommodity)
                                    </div>
                                </div>
                                <div class="form-group col-md-1">
                                    <label>是否强制上架</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsLanmaSold, new { @class = "onclickIS" })
                                    </div>
                                </div>
                                <div class="form-group col-md-1 orderBy" style=" display:none">
                                    <label>显示排序数字</label>
                                    <div class="input-group">
                                        <input type="text" id="DisplaySort" name="DisplaySort" value="@Model.DisplaySort" class="form-control">
                                    </div>
                                </div>
                                <div class="form-group col-md-1 orderBy" style="display: none">
                                    <label>商家提成佣金</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.ShopCommission, new { @class = "form-control" })
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>支持支付方式</label>
                                    <br />
                                    <label>
                                        <input type="checkbox" value="0" payway="" /> 微信支付
                                    </label>&nbsp;&nbsp;
                                    <label>
                                        <input type="checkbox" value="2" payway="" /> 现金支付
                                    </label>
                                    <input type="hidden" id="SupportPayWay" name="SupportPayWay" value="@Model.SupportPayWay" />
                                </div>
                                <div class="form-group col-md-1">
                                    <label>是否单独下单商品</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsOnlySingle)
                                    </div>
                                </div>
                                <div class="form-group col-md-1">
                                    <label>是否总部发货</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsHeadquartersSend)
                                    </div>
                                </div>
                                <div class="form-group col-md-1">
                                    <label>店铺可见商品订单</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsShopCanViewOrder)
                                    </div>
                                </div>
                                <div class="form-group col-md-1">
                                    <label>店铺可修改价格</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsShopCanModifyPrice)
                                    </div>
                                </div>
                                <div class="form-group col-md-7">
                                    <label><a data-target="#TransferOrderModal" data-toggle="modal" class="btn btn-info" onclick="transfer(19);">选择批量售卖商品</a></label>
                                    @{
                        var text = "";
                        if (Model.QuantityOfBatchSale > 0 || !string.IsNullOrEmpty(Model.CommodityLibByBatchName))
                        {
                            text = "(" + Model.CommodityLibByBatchName + ",批量售卖数量:" + Model.QuantityOfBatchSale + ")";
                        }
                                    }
                                    <label id="CommodityLibIdByBatchName" name="CommodityLibIdByBatchName">@text</label><a href="javascript:CommodityLibIdByBatchName()" id="CommodityLibIdByBatchNameCar"> 清除 </a>
                                    <div class="input-group">
                                        <input type="hidden" name="CommodityLibIdByBatch" id="CommodityLibIdByBatch" value="@Model.CommodityLibIdByBatch" />
                                        <input type="hidden" name="QuantityOfBatchSale" id="QuantityOfBatchSale" value="@Model.QuantityOfBatchSale" />
                                    </div>
                                </div>
                                <div class="form-group col-md-7">
                                    <label><a data-target="#IsVirtualModal" data-toggle="modal" class="btn btn-info" onclick="transfer(19);">添加优惠劵</a></label>

                                    <label id="CouponText" name="CouponNmaeArr">@Model.CouponNmaeArr</label>
                                    <input type="hidden" id="CouponArr" name="CouponArr" value="@Model.CouponArr" /><a href="javascript:CouponArr()" id="CouponArrCar">清除</a>
                                    <div class="input-group">
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <button id="btnSave" type="submit" class="btn btn-primary">确定并保存</button>
                                </div>
                            </fieldset>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="IsVirtualModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 600px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">优惠劵</h4>
            </div>
            <div class="modal-body">
                <div style="margin-bottom: 5px;">
                    <input type="text" class="form-control" placeholder="优惠劵名称" value="" style="width: 359px;display: inline-block" id="CouponKeyWords" />
                    <a class="btn btn-primary" onclick="searchCoupon();">查询</a>     <a class="btn btn-info" style=" width: 100px" onclick="SaveCoupon();">保存</a>
                </div>
                <div class="table-responsive table-bordered" style="max-height: 250px;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th><input type="checkbox" name="check" onclick="check_all(this, 'check');" /></th>
                                <th>优惠劵名称</th>
                                <th>优惠劵类别</th>
                                <th>库存总数量</th>
                                <th>价值</th>
                                <th>数量</th>
                                <th style="text-align: center">
                            </tr>
                        </thead>
                        <tbody id="CouponData"></tbody>
                    </table>
                    <input type="hidden" id="hidOrderId" />
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="TransferOrderModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 600px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">查询商品</h4>
            </div>
            <div class="modal-body">
                <div style="margin-bottom: 5px;">
                    <input type="text" class="form-control" placeholder="商品名称" value="冰川时代" style="width: 409px;display: inline-block" id="shopKeyWords" />
                    <a class="btn btn-primary" onclick="searchCommodityLib();">查询</a>
                </div>
                <div class="table-responsive table-bordered" style="max-height: 250px;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>商品名称</th>
                                <th>单位</th>
                                <th>品牌</th>
                                <th>数量</th>
                                <th style="text-align: center">操作</th>
                            </tr>
                        </thead>
                        <tbody id="CommodityLibData"></tbody>
                    </table>
                    <input type="hidden" id="hidOrderId" />
                </div>
            </div>
        </div>
    </div>
</div>

@section javascript{
    <script src="/Content/admin/dist/js/imageUploader.js"> </script>
    <script src="/Content/admin/dist/js/areaSelectorHelper.js"></script>
    <script type="text/javascript">
        // Image Upload
        imageUploader.uploadSingle(fileImage, btnSelect, imgThumb, '@ViewBag.theHeadPic');
        // Area Selector
        areaSelectorHelper.areas("/Base/GetAreaList", ddlProvince, ddlCity, ProducingAreaId, '@Model.AreaInfo.ProvinceId', '@Model.AreaInfo.CityId', '@Model.AreaInfo.Id');
 
        $(function () {
            $("#IsLanmaSold").click(function () {
                showOrderBy();
            });

            var supportPayWay = $("#SupportPayWay").val().split(",");
            for (var i = 0; i < supportPayWay.length; i++) {
                if (supportPayWay[i] != "") {
                    $(":checkbox[payway][value='" + supportPayWay[i] + "']").prop("checked", true);
                }
            }

            $(":checkbox[payway]").click(function () {
                var payWay = "";
                $($(":checkbox[payway]:checked")).each(function () {
                    payWay += $(this).val() + ",";
                });
                $("#SupportPayWay").val(payWay);
            });

            showOrderBy();
            $("#frmUpdate").validate({
                rules: {
                    CommodityName: { required: true },
                    SuggestedPrice: { number: true },
                    DisplaySort: { required: true, min: 100001 }
                },
                messages: {
                    CommodityName: { required: "请填写商品名称" },
                    DisplaySort: { required: "请填写排序" }

                }
                ,
                submitHandler: function (form) {
                    var ceilingPrice = parseFloat($("#CeilingPrice").val());
                    var lowerPrice = parseFloat($("#LowerPrice").val());
                    var suggestedPrice = parseFloat($("#SuggestedPrice").val());
                  
                    if (ceilingPrice !== 0 && lowerPrice!==0&&ceilingPrice <= lowerPrice) {
                        return alertify.alert('下线价格不能大于上线价格');
                    }
                    if (lowerPrice > 0) {
                        if (suggestedPrice < lowerPrice) {
                            return alertify.alert('零售价格不能小于下线价格');
                        }
                    }
                    if (ceilingPrice > 0) {
                        if (suggestedPrice > ceilingPrice) {
                            return alertify.alert('零售价格不能大于上限价格');
                        }
                    }
                    if ($("#SupportPayWay").val() == "") {
                        return alertify.alert('至少选择一种支付方式');
                    }
                    showLoading();
                    $("#btnSave").html("正在保存，请勿做其他操作...").attr("disabled", "disabled");
                    form.submit();
                }
            });

            if ($("#CouponArr").val() == '') {
                $("#CouponArrCar").hide();
            }
            if ($("#CommodityLibIdByBatchName").html() == '') {

                $("#CommodityLibIdByBatchNameCar").hide();
            } else {
                $("#SuggestedPrice").attr("readOnly", true);
            }
            
            searchCoupon();
            searchCommodityLib();

        });

        function searchCommodityLib() {
            $("#CommodityLibData").html("");
            $.post("@Url.Action("GetCommodityLibList")", "&keyWords=" + encodeURIComponent($("#shopKeyWords").val()), function (data) {
                if (data.Status) {
                    for (var i = 0; i < data.Data.length; i++) {
                        $("#CommodityLibData").append("<tr><td>" + data.Data[i].CommodityName + "</td><td>" + data.Data[i].Unit + "</td><td>" + data.Data[i].BrandName + "</td><td><input type='text' id='QuantityOfBatchSale" + data.Data[i].Id + "' style='width:50px'><input type='hidden' id='CommodityName" + data.Data[i].Id + "' value=" + data.Data[i].CommodityName + " style='width:50px'><input type='hidden' id='SuggestedPrice" + data.Data[i].Id + "' value=" + data.Data[i].SuggestedPrice + "></td><td align='center'><a class='btn btn-info' onclick='selectCommdityId(" + data.Data[i].Id + ");'>选择</a></td></tr>");
                    }
                } else {
                    alertify.alert(data.Message);
                }
            });
        }

        function selectCommdityId(commodityLibIdByBatch) {
            if (!isNaN($("#QuantityOfBatchSale" + commodityLibIdByBatch).val())) {

            } else {
                return alertify.alert('请填写正确的数量');
            }
            if ($("#QuantityOfBatchSale" + commodityLibIdByBatch).val() == null || $("#QuantityOfBatchSale" + commodityLibIdByBatch).val() < 1) {
                return alertify.alert("数量必须大于0");
            }
            $("#SuggestedPrice").val($("#SuggestedPrice" + commodityLibIdByBatch).val()).attr("readOnly", true);
            $("#QuantityOfBatchSale").val($("#QuantityOfBatchSale" + commodityLibIdByBatch).val());
            $("#CommodityLibIdByBatch").val(commodityLibIdByBatch);
            $("#CommodityLibIdByBatchName").html("(" + $("#CommodityName" + commodityLibIdByBatch).val() + ",售卖数量:" + $("#QuantityOfBatchSale" + commodityLibIdByBatch).val() + ")");
            $("#CommodityLibIdByBatchNameCar").show();
            $("#TransferOrderModal").modal("hide");
        }

        function searchCoupon() {
            $("#CouponData").html("");
            $.post("@Url.Action("GetCouponList")", "&keyWords=" + encodeURIComponent($("#CouponKeyWords").val()), function (data) {

                if (data.Status) {
                    for (var i = 0; i < data.Data.length; i++) {
                        $("#CouponData").append("<tr><td><input type='checkbox' name='check' title=" + data.Data[i].CouponName + " id='check' value=" + data.Data[i].Id + " onclick='setSelectAll();'/></td><td>" + data.Data[i].CouponName + "</td><td>" + data.Data[i].CouponCategoryName + "</td><td>" + data.Data[i].TotalQuantity + "</td><td>" + data.Data[i].CardValue + "</td><td><input type='text' id='CouponNumber" + data.Data[i].Id + "' value='' style='width:50px'></td></tr>");
                    }
                } else {
                    alertify.alert(data.Message);
                }
            });
        }

        function SaveCoupon() {
            var temp = document.getElementsByName('check');
            var idStr = "";
            var nameStr = "";
            for (var i = 1; i < temp.length; i++) {
                if (temp[i].checked == true) {
                    var thisNumber = $("#CouponNumber" + temp[i].value).val();
                    if (!isNaN(thisNumber)) {

                    } else {
                        return alertify.alert('请填写正确的数量');
                    }
                    if (thisNumber < 1) {
                        return alertify.alert('优惠劵数量必须大于0');
                    }
                    nameStr += temp[i].title + "/数量:" + thisNumber + ",";
                    idStr += temp[i].value + "|" + thisNumber + ",";
                }
            }
            if (idStr == "") {
                return alertify.alert("请选择优惠劵");
            }

            $("#CouponText").html("(" + nameStr.substring(0, nameStr.length - 1) + ")");
            $("#CouponArr").val(idStr);
            $("#CouponArrCar").show();
            $("#IsVirtualModal").modal("hide");
        }

        function check_all(obj, cName) {
            var checkboxs = document.getElementsByName(cName);
            for (var i = 0; i < checkboxs.length; i += 1) {
                checkboxs[i].checked = obj.checked;
            }
        }

        function CommodityLibIdByBatchName() {
            $("#SuggestedPrice").attr("readOnly", false);
            $("#CommodityLibIdByBatchName").html("");
            $("#CommodityLibIdByBatch").val("");
            $("#QuantityOfBatchSale").val("");
            $("#CommodityLibIdByBatchNameCar").hide();
        }

        function CouponArr() {
            $("#CouponText").html("");
            $("#CouponArr").val("");
            $("#CouponArrCar").hide();
        }

        function showOrderBy() {
            if ($("#IsLanmaSold").is(':checked')) {
                $(".orderBy").show();
            } else {
                $(".orderBy").hide();
            }
        }

    </script>
}